﻿/*
=======================================================
    Wirerope【钢丝绳】
=======================================================
*/

namespace Services
{
    public interface IWireropeMapping : IMapping<Wirerope, WireropeModel, WireropeFilterModel>
    { }

    public class WireropeMapping : AutoWireropeMapping, IWireropeMapping
    {
        private readonly ILoginUserDataPermission _ILoginUserDataPermission;

        public WireropeMapping(ICurrentUser currentUser, ILoginUserDataPermission iLoginUserDataPermission)
            : base(currentUser)
        {
            _ILoginUserDataPermission = iLoginUserDataPermission;
        }

        #region 读数据
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entity">Entity对象</param>
        /// <returns></returns>
        public override WireropeModel Entity2Model(Wirerope entity)
        {
            if (entity == null)
            {
                return null;
            }

            WireropeModel model = base.Entity2Model(entity);


            /***************************************** 下面处理自定义转换 *****************************************/



            return model;
        }
        #endregion

        #region 写数据
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="model">ViewModel对象</param>
        /// <returns></returns>
        public override Wirerope Model2Entity(WireropeModel model)
        {
            Wirerope entity = base.Model2Entity(model);


            /***************************************** 下面处理自定义转换 *****************************************/



            return entity;
        }
        #endregion

        #region 读数据(集合)
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entities">Entity对象</param>
        /// <returns></returns>
        public override List<WireropeModel> Entity2Model(IEnumerable<Wirerope> entities)
        {
            return entities.Select(x => Entity2Model(x)).ToList();
        }
        #endregion

        #region 写数据(集合)
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="models">ViewModel对象</param>
        /// <returns></returns>
        public override List<Wirerope> Model2Entity(IEnumerable<WireropeModel> models)
        {
            return models.Select(x => Model2Entity(x)).ToList();
        }
        #endregion

        #region 搜索条件转换
        /// <summary>
        /// 搜索条件转换，从FilterModel对象转换成Where对象
        /// </summary>
        /// <param name="filterModel">FilterModel对象</param>
        /// <returns></returns>
        public override Expressionable<Wirerope> FilterModel2Where(WireropeFilterModel filterModel)
        {
            var where = base.FilterModel2Where(filterModel);

            //处理数据权限
            if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.集团)
            {
                if (_currentUser.CurrentUserInfo.LoginUserRoleDepartment.Item1 == false)
                {
                    var pkIDs = _ILoginUserDataPermission.GetTablePkIDs(DataDepartment_Type.钢丝绳管理, null, _currentUser.CurrentUserInfo.LoginUserRoleDepartment.Item2.ToArray());
                    where = where.And(x => pkIDs.Contains(x.WireropeID));
                }
            }

            //SearchInput
            if (!string.IsNullOrEmpty(filterModel._SearchInput))
            { }

            //规格型号
            if (!string.IsNullOrEmpty(filterModel._GuiGe))
            {
                where = where.And(x => x.GuiGe.Contains(filterModel._GuiGe));
                //where = where.And(x => x.GuiGe == filterModel._GuiGe);
            }

            ////直径
            //if (filterModel._Diameter.HasValue)
            //{
            //    where = where.And(x => x.Diameter == filterModel._Diameter.Value);
            //}

            ////绳子条数
            //if (filterModel._Cnt.HasValue)
            //{
            //    where = where.And(x => x.Cnt == filterModel._Cnt.Value);
            //}

            ////创建人
            //if (filterModel._CreateUserID.HasValue)
            //{
            //    where = where.And(x => x.CreateUserID == filterModel._CreateUserID.Value);
            //}

            ////修改人
            //if (filterModel._UpdateUserID.HasValue)
            //{
            //    where = where.And(x => x.UpdateUserID == filterModel._UpdateUserID.Value);
            //}


            return where;
        }
        #endregion
    }
}
